<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <el-button slot="after" type="primary" size="samll" @click="addPermission(1,0)">新增权限</el-button>
      </PageTools>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog :visible="showDialog" :title="formData.id?'编辑权限':'新增权限'" @close="close">
        <el-form
          ref="perFormRef"
          :model="formData"
          :rules="rules"
          size="medium"
          label-width="120px"
          label-position="left"
        >
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入权限名称" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" placeholder="请输入权限标识" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" placeholder="请输入权限描述" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0"></el-switch>
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission, updatePermission, getPermissionDetail, addPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  components: {},
  props: {},
  data() {
    return {
      list: [],
      formData: {
        name: undefined,
        code: undefined,
        description: undefined,
        enVisible: '0',
        type: undefined,
        pid: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入权限名称',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入权限标识',
          trigger: 'blur'
        }]
      },
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  },
  mounted() {},
  methods: {
    // 编辑更新操作
    async editPermission(id) {
      const res = await getPermissionDetail(id)
      console.log(res)
      this.formData = res
      this.showDialog = true
    },
    // 删除操作
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗?')
        // 确认删除
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功!')
      } catch (error) {
        // 取消删除
        console.log(error)
        this.$message.success('已取消')
      }
    },
    // 添加操作
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 确认操作
    btnOk() {
      this.$refs.perFormRef.validate(async val => {
        console.log(val)
        if (!val) return
        if (this.formData.id) {
          // 有id是编辑
          await updatePermission(this.formData)
        } else {
          // 无id是新增
          await addPermission(this.formData)
        }
        this.$message.success('新增成功！')
        this.getPermissionList()
        this.close()
      })
    },
    async getPermissionList() {
      // 扁平化数据转树型结构
      this.list = tranListToTreeData(await getPermissionList(), '0')
    },
    onOpen() { },
    close() {
      this.formData = this.$options.data().formData
      this.$refs['perFormRef'].resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
